using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;

namespace Tryit.Views
{
    public partial class ExportDialog : Window
    {
        public int? SelectedYear { get; private set; }
        public int? SelectedMonth { get; private set; }
        public string SelectedBrand { get; private set; } // 新增属性

        public ExportDialog()
        {
            InitializeComponent();

            // 初始化年份下拉框（当前年份前后5年）
            var currentYear = DateTime.Now.Year;
            var years = Enumerable.Range(currentYear - 5, 11).ToList();
            YearComboBox.ItemsSource = years;
            YearComboBox.SelectedItem = currentYear;

            // 初始化月份下拉框
            var months = Enumerable.Range(1, 12).Select(m => new { Value = m, Display = $"{m}月" }).ToList();
            MonthComboBox.ItemsSource = months;
            MonthComboBox.DisplayMemberPath = "Display";
            MonthComboBox.SelectedValuePath = "Value";

            // 初始化牌号下拉框
            var brands = new List<string> { "全部" };
            try
            {
                using (var db = new SanmaoTools.Common.Untils.SqlSugarHelper().GetInstance())
                {
                    var brandList = db.Queryable<SanmaoTools.Models.BrandAndStoveNumber>()
                        .Select(it => it.Name)
                        .ToList()
                        .Where(x => !string.IsNullOrWhiteSpace(x))
                        .Select(x => {
                            var parts = x.Split('-');
                            return parts.Length >= 2 ? ($"{parts[0]}-{parts[1]}") : x;
                        })
                        .Distinct()
                        .ToList();
                    brands.AddRange(brandList);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"加载牌号失败：{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            BrandComboBox.ItemsSource = brands;
            BrandComboBox.SelectedIndex = 0;
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            if (YearComboBox.SelectedItem == null)
            {
                MessageBox.Show("请选择年份", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }

            SelectedYear = (int)YearComboBox.SelectedItem;
            SelectedMonth = MonthComboBox.SelectedItem != null ? (int)MonthComboBox.SelectedValue : null;
            SelectedBrand = BrandComboBox.SelectedItem != null ? BrandComboBox.SelectedItem.ToString() : "全部";

            DialogResult = true;
            Close();
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = false;
            Close();
        }
    }
} 